package com.qf.java2107.yb.hotel.service.impl;

import com.qf.java2107.yb.hotel.common.ResultVO;
import com.qf.java2107.yb.hotel.dao.UserDao;
import com.qf.java2107.yb.hotel.entity.BeanFactory;
import com.qf.java2107.yb.hotel.pojo.User;
import com.qf.java2107.yb.hotel.service.UserService;
import com.qf.java2107.yb.hotel.utils.MD5Utils;

/**
 * @author yb
 * @version 1.0
 * @date 2021-10-27
 **/
public class UserServiceImpl implements UserService {

    private UserDao userDao = (UserDao) BeanFactory.getBean("userDao");

    @Override
    public ResultVO login(String username, String password) {

        try {
            User user = userDao.findByUsername(username);
            if(null == user) {
                return ResultVO.error("该用户不存在，请先注册！！");
            }
            if(user.getIsDelete().intValue() == 1) {
                return ResultVO.error("该用户已经被删除，请重新注册！！");
            }
            if(user.getUserStatus().intValue() == 0) {
                return ResultVO.error("该用户未激活，请激活后再登录！！");
            }


            //判断密码
            String pagePwd = MD5Utils.md5(password);
            if(!user.getPassword().equals(pagePwd)) {
                return ResultVO.error("用户名或密码错误，请重新输入！！");
            }
            System.out.println("--------------------");

            return ResultVO.ok("登录成功", user);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.error("该用户未激活，请激活后再登录！！");
        }

    }

    @Override
    public ResultVO logout() {
        return null;
    }
}
